/**
 * 添加或者修改页面
 */
var StoreInfoDlg = {
    data: {
        storeName: "",
        addressId: "",
        addressDetail: "",
        cash: "",
        acountNum: "",
        picPath: "",
        accessoryPath: ""
    }
};

layui.use(['form', 'admin', 'ax','layedit','table','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layedit = layui.layedit;
    var table = layui.table;
    var upload = layui.upload;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/store/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/store'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


    //MAP select 高德地图选择经纬度
    $("#mapSelect").click(function () {
        var index = layer.open({
            title: "高德地图",
            skin: 'layui-layer-admin',
            type: 2,
            content: Feng.ctxPath + '/table_select/map_select',
            area: ['100%', '100%'],
            maxmin: true,
            end: function () {
                var lnglat = admin.getTempData('lnglat');
                $("#mapSelect").val(lnglat);

            }
        });
    });



    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/store'
    });







    //图片上传1
    var uploadView = $('#imageList')
        , uploadListIns = upload.render({
        elem: '#selectFile'
        , url: Feng.ctxPath + '/productPic/upload'
        , accept: 'image'
        , multiple: true
        , auto: false
        , bindAction: '#start'
        , choose: function (obj) {
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + '<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">' + '</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                uploadView.append(tr);
            });
        }
        , done: function (res, index, upload) {
            console.log($("#picPath").val());
            if (res.code === 200) { //上传成功
                $("#picPath").val(res.data);
                // 将数据放到input框中
                var tr = uploadView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            var tr = uploadView.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });








    //附件上传
    var uploadView2 = $('#imageList2')
        , uploadListIns2 = upload.render({
        elem: '#selectFile2'
        , url: Feng.ctxPath + '/zApplication/UploadFileShi'
        , accept: 'file'
        , multiple: true
        , auto: false
        , bindAction: '#start2'
        , choose: function (obj) {
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + '<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">' + '</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                uploadView2.append(tr);
            });
        }
        , done: function (res, index, upload) {
            console.log($("#picPath").val());
            if (res.code === 200) { //上传成功
                $("#accessoryPath").val(res.data);
                // 将数据放到input框中
                var tr = uploadView2.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            var tr = uploadView2.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });






    //图片上传3
    var uploadView3 = $('#imageList3')
        , uploadListIns3 = upload.render({
        elem: '#selectFile3'
        , url: Feng.ctxPath + '/productPic/upload'
        , accept: 'image'
        , multiple: true
        , auto: false
        , bindAction: '#start3'
        , choose: function (obj) {
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + '<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">' + '</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                uploadView3.append(tr);
            });
        }
        , done: function (res, index, upload) {
            console.log($("#picPath").val());
            if (res.code === 200) { //上传成功
                $("#paperPath").val(res.data);
                // 将数据放到input框中
                var tr = uploadView3.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            var tr = uploadView3.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });





















});